/*
 * 创建日期 2011-3-3
 *
 * 成都天和软件公司
 * 电话：028-85425861 
 * 传真：028-85425861-8008 
 * 邮编：610041 
 * 地址：成都市武侯区航空路6号丰德万瑞中心B座1001 
 * 版权所有
 */
package  org.fantasy.common.db.pool;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 连接池
 * 
 * @author 王文成
 * @version 1.0
 * @since 2011-3-24
 */
public class DataSourcePool {

	private static final Log log = LogFactory.getLog(DataSourcePool.class);

	/**
	 * 连接池Map
	 */
	private static final Map<DBOptions, DataSource> poolMap = new HashMap<DBOptions, DataSource>();

	/**
	 * 取得数据源
	 * 
	 * @return
	 * @throws Exception
	 */
	public static DataSource getDataSource(DBOptions options) {
		DataSource dataSource = null;
		synchronized (poolMap) {
			if (poolMap.containsKey(options)) {
				dataSource = poolMap.get(options);
			} else {
				dataSource = newDataSource(options);
				poolMap.put(options, dataSource);
			}
			return dataSource;
		}
	}
	
	/**
	 * 创建新id数据源
	 * @param options
	 * @return
	 */
	private static DataSource newDataSource(DBOptions options){
		if( options.isJdbc()){
			log.info("Create new JdbcDataSource![" + options + "]");
			return new JdbcDataSource(new ConnectionManager(options));
		}else if( options.isOlap()){
			log.info("Create new OlapDataSource![" + options + "]");
			return new OlapDataSource(new ConnectionManager(options));
		}
		throw new RuntimeException("无效的数据源类型[options=" + options + "]");
	}

	public static void main(String[] args) {
		Object value = 10;
		if (value instanceof Integer) {
			Number v = (Number) value;
			v.intValue();
		}
	}

}